package eerbaar.action.internal.lib;

import java.util.Map;

import eerbaar.area.IArea;
import eerbaar.context.IContext;
import eerbaar.context.IParams;
import eerbaar.context.ParamFactory;
import eerbaar.elements.IElement;
import eerbaar.game.IGame;

public class RemoveElementAction extends AbstractAction {

    private static final String[] paramNames = new String[] { //
    "name", //
    };

    private String name = "bone";

    private IParams params;

    public RemoveElementAction() {
        super("removeElement");
        this.params = ParamFactory.createParams();

    }

    public RemoveElementAction(IParams params) {
        super("removeElement");
        this.params = params;

        setup(params);
    }

    @Override
    public void execute(IContext model) {

        IGame game = (IGame) model;
        IArea area = game.getArea();
        IElement[] a = area.getElements();
        for (IElement e : a) {

            if (e.getName().equals("pet")) {
                continue;
            }

            if (this.name == null || e.getName().startsWith(this.name)) {
                area.removeElement(e);
            }
        }
    }

    @Override
    public void execute(IContext model, Map<String, String> map) {
        execute(model);
    }

    @Override
    public String[] getParamNames() {

        return paramNames;
    }

    @Override
    public IParams getParams() {

        return this.params;
    }

    @Override
    public void setParams(IParams params) {
        this.params = params;
        setup(params);
    }

    private void setup(IParams params) {

        this.name = params.get("name");
    }
}
